import Taro from '@tarojs/taro'
import {redirectWithAuth, toast} from "../../../../utils";
import {getGlobalData, setGlobalData} from "../../../../global.d";
import {getUserById} from "../../../../service";
import Envs from '../../../../constant/envs'

const db = Taro.cloud.database();

/**
 * 更新用户详细信息
 * @param userId
 * @param userDetail
 */
export function updateUserDetail(userId, userDetail) {
  db.collection('user')
    .doc(userId)
    .update({
      data: {
        userDetail,
        updateTime: new Date()
      }
    })
    .then(async res => {
      console.log(res);
      toast('保存成功');
      const user = await getUserById(userId);
      setGlobalData('user', user);
      addUserIntoMain(user)
      redirectWithAuth('/pages/my/personInfo/index');
    })
    .catch(err => {
      console.error(err);
    })
}

function addUserIntoMain(user) {
  Taro.cloud.callFunction({
    name:'addUserIntoClass',
    data:{
      env:Envs.current,
      user,
      classId:'0ec685215e4a37a3110826155506f554'
    }
  })
}
